package com.fingard.dsp.bank.directbank.xwbank02;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.xwbank.sdk.XWBankPay;
import com.xwbank.sdk.entity.UnbindBankCardInfo;

public class XWBank02DeContract extends XWBank02Base implements IDeContract {
	@Override
	public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
		RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
		RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
		tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

		try {
			String propertiesPath = getBankConfig().getValueAsString("propertiesPath");
			String merchantId = getBankConfig().getValueAsString("merchantId");
			String ownKeyStorePath = getBankConfig().getValueAsString("ownKeyStorePath");
			if (StringHelper.isNullOrEmpty(propertiesPath) || StringHelper.isNullOrEmpty(merchantId) || StringHelper.isNullOrEmpty(ownKeyStorePath)) {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "缺失'propertiesPath/merchantId/ownKeyStorePath'配置或节点内容为空!";
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			
			boolean initSuccess = XWBankPay.init(getConfigPath(propertiesPath), getConfigPath(ownKeyStorePath));
			if (!initSuccess) {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "config.properties配置有误!";
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			
			UnbindBankCardInfo deContract = new UnbindBankCardInfo();
			
			deContract.setVersion("1.0");
			deContract.setMcht_no(merchantId);
			deContract.setTran_no(p_req.contractDetail.askNumber);
			deContract.setSource_type("");
			deContract.setConsumer_id("");
			deContract.setAcct_no(p_req.contractDetail.payAct);
			
			writeBankLogStep2(deContract);
			
			Map<String, Object> tmpMapRet = XWBankPay.getInstance().unbindBankCard(deContract);
			
			writeBankLogStep3(tmpMapRet);
			
			if (tmpMapRet != null) {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
				tmpRetDetail.respInfo = (String) tmpMapRet.get("returnMsg");
				
				String tmpRespCode = (String) tmpMapRet.get("returnCode");
				if ("000000".equals(tmpRespCode)) {
					JSONObject tmpJsonRet = JSONObject.parseObject((String) tmpMapRet.get("data"));
					
					String returnMsg = tmpJsonRet.getString("return_message");
					if (StringHelper.hasAnyChar(returnMsg)) {
						tmpRetDetail.respInfo = returnMsg;
					}
					
					String result = tmpJsonRet.getString("result");
					if ("0".equals(result)) {
						tmpRetDetail.deSignState = ContractState.SUCCESS;
					} else if ("1".equals(result)) {
						tmpRetDetail.deSignState = ContractState.FAIL;
					} else {
						tmpRetDetail.deSignState = ContractState.SIGNING;
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = (String) tmpMapRet.get("returnMsg");
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "银行返回内容为空!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
		RetAtsDeContract retAts = new RetAtsDeContract(p_reqQry.reqHeader);
		
		RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
		tmpRetDetail.respCode = "-1";
		tmpRetDetail.respInfo = "渠道方不支持该交易类型:" + p_reqQry.reqHeader.transType;
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
}
